package com.jit.monitor.base;

import com.google.gson.Gson;
import com.jit.baselib.bean.response.BaseListResponse;
import com.jit.baselib.bean.response.BaseResponse;
import com.jit.monitor.commom.AppConstant;
import com.jit.monitor.model.bean.CmBndBean;
import com.jit.monitor.model.bean.EarlyWarning;
import com.jit.monitor.model.cache.UserCache;
import com.jit.monitor.model.request.WarnRequest;
import com.jit.monitor.model.response.CtFrameResponse;
import com.jit.monitor.model.response.MainResponse;
import com.jit.monitor.model.response.WarnHistoryBean;

import io.reactivex.Observable;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.POST;
import retrofit2.http.Query;

/**
 * @author crazyZhangxl on 2018/11/1.
 * Describe: 该retrofit默认添加了请求token
 */
public class ApiRetrofit extends WithHeadClient {
    public MyApi mApi;
    private ApiRetrofit(){
        //在构造方法中完成对Retrofit接口的初始化
        mApi = new Retrofit.Builder()
                .baseUrl(MyApi.BASE_URL)
                .client(getClient())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()
                .create(MyApi.class);
    }

    private static class  ApiRetrofitHolder{
        private static final ApiRetrofit M_INSTANCE =  new ApiRetrofit();
    }

    public static ApiRetrofit getInstance(){
        return ApiRetrofitHolder.M_INSTANCE;
    }



    public Observable<BaseResponse> requestChangePwd(String newPwd){
        return mApi.requestChangePwd(UserCache.getUserName(),UserCache.getUserPwd(),newPwd);
    }


    public Observable<BaseListResponse<MainResponse>> getCameraAndCurrentFrameInfo(int page){
        return mApi.getCameraAndCurrentFrameInfo(page);
    }

    public Observable<BaseListResponse<CtFrameResponse>> getCurrentDetailDetInfo(String frameId, String objDetType){
        return mApi.getCurrentDetailDetInfo(frameId,objDetType);
    }

    public  Observable<BaseResponse> editCameraName(String cameraId,String cameraName){
        return mApi.editCameraName(cameraId,cameraName);
    }

    public Observable<BaseResponse> setAlarmArea(WarnRequest warnRequest){
        return mApi.setAlarmArea(getRequestBody(warnRequest));
    }

    public Observable<BaseResponse> setAlarmRing(int userID,int warnType){
        return mApi.setAlarmRing(userID,warnType);
    }

     public Observable<BaseResponse<Integer>> queryAlarmRing(){
        return mApi.queryAlarmRing(UserCache.getUserID());
     }


    public Observable<BaseListResponse<EarlyWarning>> queryAlarmRange(){
        return mApi.queryAlarmRange(UserCache.getUserID());
    }

    public Observable<BaseResponse> controlNotificationByInfo(String cameraID ,String flag){
        return mApi.controlNotificationByInfo(cameraID,flag);
    }

    public  Observable<BaseResponse> controlLedState(String cameraID ,String flag){
        return mApi.controlLedState(cameraID,flag);
    }

    public  Observable<BaseListResponse<WarnHistoryBean>> queryWnHsyByCameraID(String cameraId,int page){
        return mApi.queryWnHsyByCameraID(cameraId,page, AppConstant.PAGE_SIZE);
    }


    /**
     * 执行绑定摄像头 -----
     * @param cmBndBean
     * @return
     */
    public Observable<BaseResponse> boundCamera(CmBndBean cmBndBean){
        return mApi.boundCamera(getRequestBody(cmBndBean));
    }

    /**
     * bean 转body
     * @param obj
     * @return
     */
    private RequestBody getRequestBody(Object obj) {
        String route = new Gson().toJson(obj);
        return RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),route);
    }
}
